/**
 * 出库主表js
 */

$(function () {
    $('#select2Id').selectBindEx({
        url: "/sys/macro/value?value=客户",
        async: true,
        text: 'name',
        value: 'macroId',
        placeholder: '请选择客户...',
        selected: '',
        allowClear: false,
        theme: "bootstrap",
        language: "zh-CN",
        change: function(){
            vm.keyword = $("#select2Id option:selected").val();
        }
    });

    $('#select2Type').selectBindEx({
        url: "/sys/macro/value?value=出库类型",
        async: true,
        text: 'name',
        value: 'macroId',
        placeholder: '请选择出库类型...',
        selected: '',
        allowClear: false,
        theme: "bootstrap",
        language: "zh-CN",
        change: function(){
            vm.outTypeCode = $("#select2Type option:selected").val();
        }
    });

    $('#select2Dept').selectBindEx({
        url: "/sys/macro/value?value=库房",
        async: true,
        text: 'name',
        value: 'macroId',
        placeholder: '请选择库房...',
        selected: '',
        allowClear: false,
        theme: "bootstrap",
        language: "zh-CN",
        change: function(){
            vm.deptCode = $("#select2Dept option:selected").val();
        }
    });

    $('#select2Item').selectBindEx({
        url: "/sys/macro/value?value=项目",
        async: true,
        text: 'name',
        value: 'macroId',
        placeholder: '请选择项目...',
        selected: '',
        allowClear: false,
        theme: "bootstrap",
        language: "zh-CN",
        change: function(){
            vm.itemCode = $("#select2Item option:selected").val();
        }
    });


    initialPage();
	getGrid();
});

function initialPage() {
	$(window).resize(function() {
		$('#dataGrid').bootstrapTable('resetView', {height: $(window).height()-56});
	});
    //日期选择
    laydate.render({
        elem: '#dateRange',
        range: true,
        theme: '#3C8DBC',
        eventElem: '#dateRange',
        trigger: 'click',
        done: function(value, date, endDate){
            vm.dateRangeSelect(0);
            vm.dateRange = value;
            vm.startDate = formatDate(date.year + '-' + date.month + '-' + date.date, 'yyyy-MM-dd');
            vm.endDate = formatDate(endDate.year + '-' + endDate.month + '-' + endDate.date, 'yyyy-MM-dd');
        }
    });
}

function getGrid() {
	$('#dataGrid').bootstrapTableEx({
		url: '../../outbound/list?_' + $.now(),
		height: $(window).height()-96,
		queryParams: function(params){
            params.customer = vm.keyword;
            params.outTypeCode = vm.outTypeCode;
            params.deptCode = vm.deptCode;
            params.itemCode = vm.itemCode;
            params.productName = vm.productName;
            params.startDate = vm.startDate;
            params.endDate = vm.endDate;
			return params;
		},
		columns: [
            {
                field : "outBoundNumber",
                title : "出库单号",
                width : "200px",
                formatter : function(value, row, index) {
                    return [
                        '<a href="javascript:void(0);" style="color: #3c8dbc" title="查看出库明细" onclick="listDetails('+ row.id +')">' + row.outBoundNumber + '</a>'
                    ].join("")
                }
            },
            {field : "outBoundTime", title : "出库时间", width : "200px"},
            {field : "customerName", title : "客户", width : "200px"},
            {field : "outTypeName", title : "出库类型", width : "200px"},
            {field : "amount", title : "金额", width : "150px",
                formatter : function (value, row, index) {
                    return value.toFixed(2);
                }
            },
            {field : "outBoundPeople", title : "出库人", width : "100px"},
            {title : "操作", width : "80px", formatter : function(value, row, index) {
                    var _html = '';
                    _html += '<a href="javascript:void(0);" style="color: #3c8dbc" onclick="getOutBound(\''+row.id+'\', ' +
                        '\''+row.outBoundNumber+'\', \''+row.outBoundTime+'\', \''+row.outBoundPeople+'\', \''+row.customerName+'\', ' +
                        '\''+row.outTypeName+'\', ' +
                        '\''+row.amount+'\')" title="打印出库单"><i class="fa fa-print"></i></a>';

                    _html += '<a href="javascript:;" onclick="vm.remove(false,\''+row.id+'\')" title="作废"><i class="fa fa-trash-o"></i></a>';
                    /*
                    if (hasPermission('project:outbound:edit')) {
                    _html += '<a href="javascript:;" onclick="vm.remove(false,\''+row.id+'\')" title="作废"><i class="fa fa-trash-o"></i></a>';
                    }
                     */
                    return _html;
                }
            }
		]
	})
}

function listDetails(id) {
    dialogOpen({
        title: '出库商品明细',
        url: 'base/outbound/details/list.html?_' + $.now(),
        width: '920px',
        height: '550px',
        scroll: true,
        btn: [],
        success: function(iframeId){
            top.frames[iframeId].vm.outBoundId = id;
            top.frames[iframeId].vm.getGrid();
        },
        yes : function(iframeId) {
            top.frames[iframeId].vm.acceptClick();
        },
    });
}

function getOutBound(id, outBoundNumber, outBoundDate, outBoundPeople, customer, outTypeCode,outBoundAmount) {
    dialogOpen({
        title: '出库单',
        url: 'base/outbound/outBound.html',
        width: '920px',
        height: '550px',
        scroll: false,
        btn: ['打印', '取消'],
        success: function(iframeId){
            top.frames[iframeId].vm.outBound.id = id;
            top.frames[iframeId].vm.outBound.outBoundNumber = outBoundNumber;
            top.frames[iframeId].vm.outBound.outBoundDate = outBoundDate;
            top.frames[iframeId].vm.outBound.outBoundPeople = outBoundPeople;
            top.frames[iframeId].vm.outBound.customer = customer;
            top.frames[iframeId].vm.outBound.outTypeCode = outTypeCode;
            top.frames[iframeId].vm.outBound.outBoundAmount = parseFloat(outBoundAmount).toFixed(2);
            top.frames[iframeId].vm.getGrid();
        },
        yes : function(iframeId) {
            top.frames[iframeId].print();
        },
    });
}

var vm = new Vue({
	el:'#dpLTE',
	data: {
        keyword: null,
        outTypeCode:null,
        deptCode:null,
        itemCode:null,
        productName:null,
        startDate : null,
        endDate : null,
        dateRangeText : '时间范围',
        dateRange : null,
	},
	methods : {
		load: function() {
            getGrid();
			$('#dataGrid').bootstrapTable('refresh');
		},
        dateRangeSelect : function(count) {
            if(count==1){
                vm.dateRangeText = '最近一天';
                vm.startDate = countDay(-1);
                vm.endDate = today();
                vm.dateRange = vm.startDate + ' - ' + vm.endDate;
            }else if(count ==7){
                vm.dateRangeText = '最近一周';
                vm.startDate = countDay(-7);
                vm.endDate = today();
                vm.dateRange = vm.startDate + ' - ' + vm.endDate;
            }else if(count ==30){
                vm.dateRangeText = '最近一月';
                vm.startDate = countDay(-30);
                vm.endDate = today();
                vm.dateRange = vm.startDate + ' - ' + vm.endDate;
            }else{
                vm.dateRangeText = '时间范围';
                vm.startDate = '';
                vm.endDate = '';
                vm.dateRange = '';
            }
        },
		save: function() {
			dialogOpen({
				title: '新增商品出库',
				url: 'base/outbound/add.html?_' + $.now(),
                width: '920px',
                height: '690px',

				yes : function(iframeId) {
					top.frames[iframeId].vm.acceptClick();
				},
			});
		},
		edit: function(id) {
            dialogOpen({
                title: '编辑出库主表',
                url: 'base/outbound/edit.html?_' + $.now(),
                width: '420px',
                height: '350px',
                success: function(iframeId){
                    top.frames[iframeId].vm.outBound.id = id;
                    top.frames[iframeId].vm.setForm();
                },
                yes: function(iframeId){
                    top.frames[iframeId].vm.acceptClick();
                }
            });
        },
        remove: function(batch, id) {
            var ids = [];
            if (batch) {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (!checkedArray(ck)) {
                    return false;
                }
                $.each(ck, function(idx, item){
                    ids[idx] = item.id;
                });
            } else {
                ids.push(id);
            }
            $.RemoveForm({
                url: '../../outbound/remove?_' + $.now(),
                param: ids,
                success: function(data) {
                    vm.load();
                }
            });
        },
	}
})